include Java

require "raw_classifier_strings"
require "classifier_pool"
require "peer_ranking_service"
require "component"
require "T:/Files/weka/dist/weka.jar"
require "running_parameters"
require "log"
require "j_time"

import "java.io.FileReader"
import "weka.core.Instances"


#
puts( "<prepare train, query and test>" )

data_set_root_folder = "T:/Files/uci_binary_datasets"
splited_data_set_root_folder = "#{data_set_root_folder}/3_1_1"

train_file = "#{splited_data_set_root_folder}/#{RunningParameters.data_set}/#{RunningParameters.current_fold}/training.arff"
train = Instances.new( FileReader.new( train_file ) )
train.setClassIndex( train.numAttributes - 1 )

query_file = "#{splited_data_set_root_folder}/#{RunningParameters.data_set}/#{RunningParameters.current_fold}/query.arff"
query = Instances.new( FileReader.new( query_file ) )
query.setClassIndex( query.numAttributes - 1 )

test_file = "#{splited_data_set_root_folder}/#{RunningParameters.data_set}/#{RunningParameters.current_fold}/test.arff"
test = Instances.new( FileReader.new( test_file ) )
test.setClassIndex( test.numAttributes - 1 )



#
Log.initialize()
parameter_str = "Interaction model, #{RunningParameters.im}, Population size, #{RunningParameters.pool_size}, \
Number of members of ensemble classifier, #{RunningParameters.num_of_ensemble_classifier_members}, \
Data set, #{RunningParameters.data_set}, Num of rows of scores: #{query.numInstances + 1}"
Log.write( 0, parameter_str )



#
RawClassifierStrings.set_raw_classifier_strings()
puts( "<num_of_raw_classifier_strings>" ); puts( RawClassifierStrings.num_of_raw_classifier_strings )




# 
pool = ClassifierPool.new( RunningParameters.random_seed, RunningParameters.pool_size ) # random seed, pool size
puts( "<pool.members>" ); puts( pool.members )
puts( "<pool.num_of_members>" ); puts( pool.num_of_members )

member_ids = []; pool.num_of_members.times { | i | member_ids << "p#{i}" }
Log.write( 0, member_ids.join( ',' ) )
Log.write( 0, pool.members.join( ',' ) )



#
PeerRankingService.init( pool, RunningParameters.prs_initial_seed )
puts( "<PeerRankingService.scores>" ); puts( PeerRankingService.scores )
Log.write( 0, PeerRankingService.scores.join( ',' ) )
Log.write( 2, PeerRankingService.ratings.join( ',' ) )



##
component = Component.new( RunningParameters.im )

# query
for i in 0..( query.numInstances - 1 )
  instance_query = Instances.new( query, i, 1 ) # source, first, toCopy

  # 1
  time1 = Time.new
  result_value = component.get_result_value(
    pool, RunningParameters.num_of_ensemble_classifier_members, train, instance_query, "pctCorrect()", "query" )
  time2 = Time.new
  puts( "<2 query[ #{i} ] - last_recommended_peers>" ); puts( PeerRankingService.last_recommended_peers )
  if JTime.time_ok?( time2 - time1 ) == true
    case result_value
    when 100
      PeerRankingService.update_last_recommended_peers( '+' )
    else
      PeerRankingService.update_last_recommended_peers( '-' )
    end
  else
    PeerRankingService.update_last_recommended_peers( '-' )
  end
  # Log.write( 1,
  if i == 0 then
    Log.write( 1, PeerRankingService.last_recommended_peers.join( ',' ) )
    result_value = component.get_result_value( pool, RunningParameters.num_of_ensemble_classifier_members,
      train, test, "pctCorrect()", "test", PeerRankingService.last_recommended_peers )
    Log.write( 1, "pctCorrect(): #{result_value}" )
    result_value = component.get_result_value( pool, RunningParameters.num_of_ensemble_classifier_members,
      train, test, "weightedFMeasure()", "test", PeerRankingService.last_recommended_peers )
    Log.write( 1, "weightedFMeasure(): #{result_value}" )
    result_value = component.get_result_value( pool, RunningParameters.num_of_ensemble_classifier_members,
      train, test, "weightedAreaUnderROC()", "test", PeerRankingService.last_recommended_peers )
    Log.write( 1, "weightedAreaUnderROC(): #{result_value}" )
  end
  #
  # 2
  time1 = Time.new
  result_value = component.get_result_value(
    pool, RunningParameters.num_of_ensemble_classifier_members, train, instance_query, "pctCorrect()", "query" )
  time2 = Time.new
  puts( "<2 query[ #{i} ] - last_recommended_peers>" ); puts( PeerRankingService.last_recommended_peers )
  if JTime.time_ok?( time2 - time1 ) == true
    case result_value
    when 100
      PeerRankingService.update_last_recommended_peers( '+' )
    else
      PeerRankingService.update_last_recommended_peers( '-' )
    end
  else
    PeerRankingService.update_last_recommended_peers( '-' )
  end
  #

  puts( "<3 scores>" ); puts( PeerRankingService.scores )
  Log.write( 0, PeerRankingService.scores.join( ',' ) )
  Log.write( 2, PeerRankingService.ratings.join( ',' ) )


  # Log.write( 1,
  Log.write( 1, PeerRankingService.last_recommended_peers.join( ',' ) )
  result_value = component.get_result_value( pool, RunningParameters.num_of_ensemble_classifier_members,
    train, test, "pctCorrect()", "test", PeerRankingService.last_recommended_peers )
  Log.write( 1, "pctCorrect(): #{result_value}" )
  result_value = component.get_result_value( pool, RunningParameters.num_of_ensemble_classifier_members,
    train, test, "weightedFMeasure()", "test", PeerRankingService.last_recommended_peers )
  Log.write( 1, "weightedFMeasure(): #{result_value}" )
  result_value = component.get_result_value( pool, RunningParameters.num_of_ensemble_classifier_members,
    train, test, "weightedAreaUnderROC()", "test", PeerRankingService.last_recommended_peers )
  Log.write( 1, "weightedAreaUnderROC(): #{result_value}" )
  #

end



# test
result_value = component.get_result_value(
  pool, RunningParameters.num_of_ensemble_classifier_members, train, test, RunningParameters.metric, "test" )
puts( "<#{RunningParameters.metric}>" ); puts( result_value )

# Log.write( 1,
Log.write( 1, PeerRankingService.last_recommended_peers.join( ',' ) )
result_value = component.get_result_value( pool, RunningParameters.num_of_ensemble_classifier_members,
  train, test, "pctCorrect()", "test", PeerRankingService.last_recommended_peers )
Log.write( 1, "pctCorrect(): #{result_value}" )
result_value = component.get_result_value( pool, RunningParameters.num_of_ensemble_classifier_members,
  train, test, "weightedFMeasure()", "test", PeerRankingService.last_recommended_peers )
Log.write( 1, "weightedFMeasure(): #{result_value}" )
result_value = component.get_result_value( pool, RunningParameters.num_of_ensemble_classifier_members,
  train, test, "weightedAreaUnderROC()", "test", PeerRankingService.last_recommended_peers )
Log.write( 1, "weightedAreaUnderROC(): #{result_value}" )


#
puts( "<Log.show()>" )
Log.show( 0 ); Log.show( 1 ); Log.show( 2 )
puts( "<Log.save()>" )
Log.save( 0 ); Log.save( 1 ); Log.save( 2 )

